<?php

namespace App\Logic\Admin;

use App\Constants\ErrorCode;

class PermissionsLogic
{
    /**
     * Notes:获取权限树
     * 2025/7/15
     * @return array
     */
    public function tree()
    {
        $permissions = \App\Model\Permission::query()->orderByDesc('created_at')->get();
        return trees($permissions->toArray());
    }

    /**
     * Note:列表
     * User: Kong
     * Date: 2025/9/25
     * Time: 10:46
     * @return mixed[]
     */
    public function list()
    {
        $permissions = \App\Model\Permission::query()->orderByDesc('created_at')->get();
        return $permissions->toArray();
    }

    /**
     * Note:创建权限
     * User: Kong
     * Date: 2025/9/25
     * Time: 10:49
     * @param $params
     * @return \App\Model\Permission|\Hyperf\Database\Model\Model
     * @throws \Throwable
     */
    public function create($params)
    {
        $result = \App\Service\Admin\PermissionService::createPermission($params);
        return $result;
    }

    /**
     * Note:更新权限
     * User: Kong
     * Date: 2025/9/25
     * Time: 10:54
     * @param $params
     * @return mixed
     * @throws \Exception
     */
    public function edit($params)
    {
        $id = $params['id']??0;
        $permission = \App\Model\Permission::query()
            ->where('id', '!=', $id)
            ->when(isset($params['name']), function ($query) use ($params) {
                return $query->where('name', $params['name']);
            })
            ->when(isset($params['controller']), function ($query) use ($params) {
                return $query->where('controller', $params['controller']);
            })
            ->when(isset($params['action']), function ($query) use ($params) {
                return $query->where('action', $params['action']);
            })
            ->when(isset($params['http_method']), function ($query) use ($params) {
                return $query->where('http_method', $params['http_method']);
            })
            ->first();
        if ($permission) {
            throw new \Exception(ErrorCode::getMessage(ErrorCode::PERMISSION_EXIST));
        }
        $permission = \App\Model\Permission::query()->find($id);
        $permission->fill($params)->save();
        return $permission;
    }

    /**
     * Note:删除权限
     * User: Kong
     * Date: 2025/9/25
     * Time: 10:56
     * @param $params
     * @return bool|int|mixed|null
     * @throws \Exception
     */
    public function destroy($params)
    {
        $id = $params['id']??0;
        if ($id<=0){
            throw new \Exception('请选择条目');
        }
        $permission = \App\Model\Permission::query()->find($id);
        if (!$permission) {//可能已经删除
            throw new \Exception(ErrorCode::getMessage(ErrorCode::DATA_NOT_EXIST));
        }
        // 如果权限是一个父级权限， 则删除的时候吧所有子类也一起删除
        if ($permission->pid == 0) {
            $childrenPermissions = \App\Model\Permission::query()
                ->where('pid', $permission->id)
                ->get()->toArray();
            if (!empty($childrenPermissions)){
                \App\Model\Permission::query()->whereIn('id', array_column($childrenPermissions, 'id'))->delete();
            }
        }
        $rt = \App\Model\Permission::query()->where('id', $id)->delete();
        if (!$rt){
            throw new \Exception('删除失败');
        }
        return [];
    }
}